#include "StdAfx.h"
#include "TinySanhuiCardPool.h"
#include "VoiceCardServer.h"
#include "VoiceCardConfig.h"
#include "TinyVoiceCardServerDefine.h"
#include "ConfigWrapper.h"

CVoiceCardServer::CVoiceCardServer(void)
{
}

CVoiceCardServer::~CVoiceCardServer(void)
{
}
bool CVoiceCardServer::init(void)
{
	m_config = new CVoiceCardConfig();

	if(m_config == NULL)
		return false;

	g_Logger.selectlogType("voicecard");
	g_Logger.log(debug1,"VoiceCardServer init begin...");

	if(m_config->readConfigFile(m_config->getConfigFileName()) == false)
		return false;

	//set config global interface
	g_Config.setConfigInstance((CVoiceCardConfig*)m_config);

	//init TCP Interface
	g_TinyTCPIF.setTCPServer(&m_TCPServer);
	g_TinyTCPIF.setMsgProcFunc(this,&CVoiceCardServer::ProcessReceivedMsg);
	g_TinyTCPIF.setServerAddr(((CVoiceCardConfig*)m_config)->m_ServerIP,((CVoiceCardConfig*)m_config)->m_ServerPort);
	

	//init card driver
	
	if(g_Config.getConfig()->m_CardType == SANHUI_CARD)
	{
		m_CardPool = new CTinySanhuiCardPool();
		//m_CardPool->setTCPServer(&g_TinyTCPIF);
		return true;
	}
	g_Logger.log(debug1,"VoiceCardServer init end...");
	return false;

}

void CVoiceCardServer::ProcessReceivedMsg(CTinyMsg& inMsg,CTinyMsg& OutMsg)
{

	//log msg
	g_Logger.log(debug1,inMsg);
	switch(inMsg.getMsgIndex())
	{
	case CLIENT_ACTION:
	case VOICECARD_ACTION:
		this->m_CardPool->ProcessReceivedMsg(inMsg,OutMsg);
		break;
	default:
		break;
	};
	g_Logger.log(debug1,OutMsg);
	return ;
}